package dw.fastfile.common.dto.sync;


import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.MapUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Map;
import java.util.UUID;

/**
 * @description: 同步参照数据通用接口
 * @author: wanghao
 * @create: 2022-06-22 10:28
 **/
@Getter
@Setter
public abstract class AbstractSyncRefData extends AbstractSyncData<RefData> {
    /**
     * 数据来源表
     *
     * @return 来源表
     */
    protected abstract String getSourceTableName();

    /**
     * 用友保存表
     *
     * @return 保存表
     */
    protected abstract String getTargetTableName();

    /**
     * 源表code字段名称
     *
     * @return code名
     */
    protected abstract String sourceCodeField();

    /**
     * 源表name字段名称
     *
     * @return name名
     */
    protected abstract String sourceNameField();

    @Override
    protected String getSaveDataSql() {
        return new StringBuilder("select distinct ")
                .append(this.sourceCodeField()).append(",").append(this.sourceNameField())
                .append(" from ").append(this.getSourceTableName())
                .append(" where 1=1 and ")
                .append(this.sourceCodeField()).append(" not in ")
                .append(" (select code from ").append(this.getTargetTableName()).append(" ) ")
                .toString();
    }

    @Override
    protected RefData createDataByMap(Map<String, Object> m) {
        return new RefData(MapUtils.getString(m, this.sourceCodeField()), MapUtils.getString(m, this.sourceNameField()));
    }

    @Override
    protected void saveTarget(JdbcTemplate jdbcTemplate, RefData ref) {
        String saveDataSql = "INSERT INTO chinasoftinc_db." + this.getTargetTableName() + " " +
                " (`code`, `name`,  `tenant_id`, `pk_temp`, \n" +
                "`version`, `ytenant_id`, `enable`, `enablets`, `disablets`,\n" +
                " `create_time`, `modify_time`, `creator`, `modifier`, `id`, `pubts`) " +
                "  VALUES (?, ?, ?, NULL, \n" +
                " NULL, ?, '1', NULL, NULL, \n" +
                " now(), NULL,?, NULL, ?, now()) ";

        String id = UUID.randomUUID().toString();

        jdbcTemplate.update(saveDataSql,
                ref.getCode(), ref.getName(), super.getTenantId(), super.getYtenantId(), super.getCreator(), id);
    }

    @Override
    protected String getUpdateDataSql() {
        return "SELECT r.* FROM (\n" +
                "SELECT * FROM ( \n" +
                "SELECT DISTINCT \n" +
                "r." + this.sourceCodeField() + ", r." + this.sourceNameField() + ", ts\n" +
                "FROM " + this.getSourceTableName() + " r," + this.getTargetTableName() + " t\n" +
                "WHERE 1=1 \n" +
                "AND r." + this.sourceCodeField() + " = t.code\n" +
                "ORDER BY r.ts DESC) a\n" +
                "GROUP BY a." + this.sourceCodeField() + ") r," + this.getTargetTableName() + " t\n" +
                "WHERE r." + this.sourceCodeField() + " = t.code\n" +
                "AND r." + this.sourceNameField() + " <> t.name";
    }

    @Override
    protected void updateTarget(JdbcTemplate jdbcTemplate, RefData ref) {
        String updateDataSql = "UPDATE chinasoftinc_db." + this.getTargetTableName() +
                " SET name = " + "'" + ref.getName() + "'" +
                " WHERE code = " + "'" + ref.getCode() + "'";
        jdbcTemplate.update(updateDataSql);
    }

}
